package com.xbjtech.service;

import com.xbjtech.mapper.UserMapper;
import com.xbjtech.model.User;
import com.xbjtech.model.UserExample;
import com.xbjtech.model.UserExample.Criteria;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
    UserMapper userMapper;

	@Override
	public void insert(User user) throws Exception {
		userMapper.insert(user);
		throw new Exception();
	}

	@Override
	public User login(String username, String password) {
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andUsernameEqualTo(username)
		.andPasswordEqualTo(password);
		List<User> list = userMapper.selectByExample(userExample);
		if (null != list && list.size() > 0) {
			Iterator<User> iterator = list.iterator();
			if (iterator.hasNext()) {
				return iterator.next();
			}
		}
		return null;
	}

	@Override
	public void update(User user) {
		userMapper.updateByPrimaryKey(user);
	}
}
